package com.wyx.jdbc.PrepareStatement_;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 王艺锡
 * @version 1.0
 */
//执行dml语句
@SuppressWarnings({"all"})
public class PrepareStatement02_ {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);

        Properties properties = new Properties();
        properties.load(new FileReader("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        //执行添加语句
       /* String sql = "insert into admin values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //给?赋值
        System.out.print("请输入管理员名称:");
        String admin_name = scanner.nextLine();
        System.out.print("请输入密码:");
        String admin_pwd = scanner.nextLine();

        preparedStatement.setString(1, admin_name);
        preparedStatement.setString(2, admin_pwd);*/


        //执行修改语句
        /*String sql = "update admin set pwd = ? where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //给?赋值
        System.out.print("请输入要修改的管理员名称:");
        String admin_name = scanner.nextLine();
        System.out.print("请输入新密码:");
        String admin_pwd = scanner.nextLine();

        preparedStatement.setString(1, admin_pwd);
        preparedStatement.setString(2, admin_name);
        */

        //执行删除语句
        String sql = "delete from admin where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //给?赋值
        System.out.print("请输入要删除的管理员名称:");
        String admin_name = scanner.nextLine();

        preparedStatement.setString(1, admin_name);

        //执行语句
        int i = preparedStatement.executeUpdate();
        System.out.println(i > 0 ? "成功" : "失败");

        preparedStatement.close();
        connection.close();
    }

}
